package ar.com.cema.methodology.analyzer.ui.servlet.json;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import ar.com.cema.methodology.analyzer.json.JsonMapperFactory;
import ar.com.cema.methodology.analyzer.ui.PageDirectory;
import ar.com.cema.methodology.analyzer.ui.servlet.DependencyInjectionHttpServlet;

public abstract class JsonProviderServlet extends DependencyInjectionHttpServlet {
	private static final long serialVersionUID = 677666319152485634L;
	protected Gson gson = JsonMapperFactory.createJSonFromBuilder();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.write(getJSON(request));
		out.close();
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getServletContext().getRequestDispatcher(PageDirectory.INDEX).forward(request, response);
		return;
	}

	protected abstract String getJSON(HttpServletRequest request);
}